<template>
  <div class="app-container yy-financial-take">
    <el-form :inline="true" label-width="80px">
      <el-form-item label="申请人">
        <el-input
          v-model="queryParams.realname"
          placeholder="申请人/手机号/用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态">
        <el-select style="width: 120px;" v-model="queryParams.statusIds" @keyup.enter.native="handleQuery" placeholder="不限" clearable size="small">
          <el-option
            v-for="dict in takeStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="角色">
        <el-select style="width: 100px;" v-model="queryParams.role" @keyup.enter.native="handleQuery" placeholder="不限" clearable size="small">
          <el-option
            v-for="dict in roleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="账户状态">
        <el-select style="width: 100px;" v-model="queryParams.userStatus" @keyup.enter.native="handleQuery" placeholder="不限" clearable size="small">
          <el-option
            v-for="dict in userStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="申请时间">
        <el-date-picker
          v-model="startTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="handleDownload">
          <svg-icon icon-class="excel"></svg-icon>
          导出
        </el-button>
      </el-form-item>

    </el-form>



    <el-table v-loading="loading" :data="postList">
      <el-table-column label="用户编号" align="center" prop="idNo"/>
      <el-table-column label="申请时间" align="center" prop="createtime" :show-tooltip-when-overflow="true">
       <!-- <template slot-scope="scope">
          <span>{{ dateFormat(scope.row.createtime,'YYYY-MM-DD HH:mm') }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="角色" align="center" prop="role" :formatter="roleFormat"/>
      <el-table-column label="真实姓名" align="center" prop="realname"/>
      <el-table-column label="手机号" align="center" prop="mobile"/>
      <el-table-column label="账号类型" align="center" prop="accounttype"/>
      <el-table-column label="账号" align="center">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="账号" width="250" trigger="hover">
            <div>{{scope.row.accountno}}</div>
            <span slot="reference" v-if="scope.row.accountno" >{{ scope.row.accountno.substr(0,10)+'...' }}</span>
            <span slot="reference" v-else class="text-info">无</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="支行信息" align="center" prop="accountbranch"/>
      <el-table-column label="申请金额" align="center">
        <template slot-scope="scope">
          <div>
            {{formatCurrency(scope.row.amount+scope.row.fee)}}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="手续费" align="center">
        <template slot-scope="scope">
          <span>{{formatCurrency(scope.row.fee) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实提金额" align="center">
        <template slot-scope="scope">
          <span>{{formatCurrency(scope.row.amount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="余额类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.withdrawalBalanceType==1">原余额</span>
          <span v-if="scope.row.withdrawalBalanceType==2">创客余额</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="scope.row.status===1"  v-has-role="['yunying']" @click="handleOpen(scope.row)" size="small"
                     type="primary" plain>运营审核
          </el-button>
          <el-button v-show="scope.row.status===2"  v-has-role="['kuaiji']" @click="handleOpen(scope.row)" size="small" type="info" plain>会计审核
          </el-button>
          <el-button v-show="scope.row.status===3"  v-has-role="['caiwu']" @click="handleOpen(scope.row)" size="small" type="success" plain>出纳审核
          </el-button>
          <el-link type="primary" v-show="scope.row.status===4" @click="handleOpen(scope.row)">银行出款</el-link>
          <el-link type="danger" v-show="scope.row.status===0" @click="handleOpen(scope.row)" >出纳退款</el-link>
          <el-link type="danger" v-show="scope.row.status===6" @click="handleOpen(scope.row)" >运营拒绝</el-link>
          <el-link type="danger" v-show="scope.row.status===7" @click="handleOpen(scope.row)" >会计拒绝</el-link>
          <el-link type="danger" v-show="scope.row.status===8" @click="handleOpen(scope.row)" >出纳拒绝</el-link>
          <el-link type="success" v-show="scope.row.status===9" @click="handleOpen(scope.row)">提现成功</el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <transition name="el-zoom-in-center">
      <el-dialog :title="title" :visible.sync="open" width="1000px" @close='cancel' :close-on-click-modal="false">
        <el-steps :active="step" align-center>
          <el-step  v-for="item in userTakeLogList" :title="item.title" :description="dateFormat(item.description,'YYYY/MM/DD HH:mm')"></el-step>
        </el-steps>
        <el-divider></el-divider>
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="真实姓名:">
                {{row.realname}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="账号类型:">
                {{row.accounttype}}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="手机号:">
                {{row.mobile}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="附加支行信息:">
                {{row.accountbranch}}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="角色:">
                {{roleFormat(row)}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="账户:">
                {{row.accountno}}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="用户状态:">
                {{userStatusFormat(row)}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请提现金额:">
                ￥{{formatCurrency(row.fee+row.amount)}}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="用户编号:">
                {{row.idNo}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="手续费:">
                ￥{{formatCurrency(row.fee)}}
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="证件号码:">
                {{row.idcard}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实际提现金额:">
                ￥{{formatCurrency(row.amount)}}
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="提现审核" style="margin-top: 20px;" prop="audit" v-show="row.status!==4">
            <el-radio-group v-model="form.audit" size="medium">
              <el-radio border :label="1">通过</el-radio>
              <el-radio border :label="0">不通过</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="" style="margin-top: 20px;" v-show="!form.audit">
            <el-input v-model="form.remark" type="textarea" :rows="4" placeholder="请输入不通过理由"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer" v-show="row.status!==4">
          <el-button type="primary" :disabled="disabled" v-if="row.status!==9&&row.status!==6&&row.status!==8&&row.status!==7&&row.status!==4&&row.status!==0" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </transition>


  </div>
</template>

<script>
  import {list,updateUserTake,excelList} from "@/api/main/financial/take";

  export default {
    data() {
      return {
        step:1,
        row: '',
        title: '',
        open: false,
        startTime: '',
        loading: false,
        total: 0,
        postList: [],
        statusOptions: [],
        roleOptions: [],
        userStatusOptions: [],
        takeStatusOptions:[],
        userTakeLogList:[],
        disabled:false,
        queryParams: {
          startTime:undefined,
          endTime:undefined,
          statusIds:undefined,
          userStatus: undefined,
          mobile: '',
          pageNum: 1,
          pageSize: 10
        },
        form: {
          remark: undefined,
          audit: 1
        },
        rules: {
          audit: [
            {required: true}
          ]
        }
      }
    },
    created() {
      this.getDicts("yy_user_role").then(response => {
        this.roleOptions = response.data;
      });
      this.getDicts("yy_user_status").then(response => {
        this.userStatusOptions = response.data;
      });
      this.getDicts("yy_take_status").then(response => {
        this.takeStatusOptions = response.data;
      });
      this.getList();
    },
    methods: {
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.disabled=true;
            switch (this.row.status) {
              case 1:
                if(this.form.audit===1){
                  this.form.status=2;
                }else{
                  this.form.status=6;
                }
                break;
              case 2:
                if(this.form.audit===1){
                  this.form.status=3;
                }else{
                  this.form.status=7;
                }
                  break;
              case 3:
                if(this.form.audit===1){
                  this.form.status=4;
                }else {
                  this.form.status=8;
                }
                break;
              case 4:
                this.form.status=0;
                  break;
            }
            this.form.id=this.row.id;
            updateUserTake(this.form).then(response => {
              this.disabled=false;
              if (response.code === 200) {
                this.msgSuccess("审核成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        });
      },
      cancel() {
        this.form={
          remark: undefined,
          audit: 1
        };
        this.resetForm("form");
        this.open = false;
      },
      roleFormat(row, column) {
        return this.selectDictLabel(this.roleOptions, row.role);
      },
      userStatusFormat(row, column) {
        return this.selectDictLabel(this.userStatusOptions, row.userStatus);
      },
      statusFormat(row, column) {
        for(let i=0;i<this.takeStatusOptions.length;i++){
          if(this.takeStatusOptions[i].dictValue.indexOf(row.status+'')>=0){
            return this.takeStatusOptions[i].dictLabel;
          }
        }
        return row.status;
      },
      handleDownload(){
        let _this=this;
        this.$confirm('确定要导出excel(请先确认点击搜索按条件导出)?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        }).then(function() {
          const loading = _this.$loading({
            lock: true,
            text: '下载中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          });
          import('@/utils/excel').then(excel => {
            const tHeader = ['用户编号', '申请时间', '角色', '真实姓名', '手机号','账号类型','账号','支行信息','申请金额','手续费','实提金额','状态'];
            const filterVal = ['idNo', 'createtime', 'role', 'realname', 'mobile','accounttype','idcard','accountbranch','apply','fee','amount','status'];
            let query =  {
              startTime:_this.queryParams.startTime,
              endTime:_this.queryParams.endTime,
              statusIds:_this.queryParams.statusIds,
              userStatus: _this.queryParams.userStatus,
              mobile: _this.queryParams.mobile,
            };
            excelList(query).then(response => {
              const data = _this.formatJson(filterVal,  response.rows);
              excel.export_json_to_excel({
                header: tHeader,
                data,
                filename: '用户提现',
                autoWidth: _this.autoWidth
              });
              loading.close();
            });
          });
        }).catch(function(e) {
          console.log(e);
        });
      },
      formatJson(filterVal, list) {
        return list.map(v => filterVal.map(j => {
          if (j === 'createtime') {
            return this.dateFormat(v[j])
          }else if(j==='role'){
            return this.roleFormat(v);
          }else if(j==='status'){
            return this.statusFormat(v);
          } else if(j==='apply'){
            return v.amount+v.fee;
          }else {
            return v[j]
          }
        }))
      },
      handleQuery() {
        if (this.startTime) {
          this.queryParams.startTime = this.startTime[0] + " 00:00:00";
          this.queryParams.endTime = this.startTime[1] + " 23:59:59";
        } else {
          this.queryParams.startTime = null;
          this.queryParams.endTime = null;
        }
        this.queryParams.pageNum = 1;
        this.getList();
      },
      getList() {
        this.loading = true;
        list(this.queryParams).then(response => {
          console.log(response.rows);
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleOpen(row) {
        this.disabled=false;

        let arr=[];
        arr.push({key:0,title:'用户提交审核',description:row.createtime});
        arr.push({key:1,title:'运营审核',description:null});
        arr.push({key:2,title:'会计审核',description:null});
        arr.push({key:3,title:'出纳审核',description:null});
        arr.push({key:4,title:'银行出款中',description:null});
        arr.push({key:5,title:'提现成功',description:null});

        if(row.userTakeLogList){
          for(let i=0;i<row.userTakeLogList.length;i++){
            arr[i+1].description=row.userTakeLogList[i].createtime;
          }
        }

        switch (row.status) {
          case 0:
            this.form.audit=0;
            this.form.remark=row.remark;
            this.step=5;
            break;
          case 6:
            this.form.audit=0;
            this.form.remark=row.remark;
            this.step=1;
            break;
          case 7:
            this.form.audit=0;
            this.form.remark=row.remark;
            this.step=2;
            break;
          case 8:
            this.form.audit=0;
            this.form.remark=row.remark;
            this.step=3;
            break;
          default:
            this.step=row.status;
            break;
        }
        this.userTakeLogList=arr;
        this.row = row;
        this.title = '提现审核';
        this.open = true;
      }
    }
  }
</script>

<style scoped>
  .yy-financial-take .el-dialog__wrapper .el-form-item {
    margin-bottom: 0;
  }

  .yy-financial-take .dialog-footer {
    text-align: center;
  }
</style>
